package com.sptpc.demo.controller;

import com.sptpc.demo.controller.requests.AddOrderRequests;
import com.sptpc.demo.controller.requests.ModifyOrderRequest;
import com.sptpc.demo.model.Order;
import com.sptpc.demo.repository.OrderRepository;
import com.sptpc.demo.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private IOrderService orderService;

    @PostMapping("/")
    public Order addNewOrder(@RequestBody AddOrderRequests requests){
        log.info("订单信息:{}",requests);
        Order order = new Order();
        BeanUtils.copyProperties(requests,order);
        orderService.addOrder(order,requests.getItems());
        return order;
    }
    //get//order
    @GetMapping("/{customer}")
    public List<Order> getOrdersByCustomer(@PathVariable String customer){
        return orderService.getOrdersByCustomer(customer);

    }
    //删除订单
    @DeleteMapping("/{id}")
    public String deleteOrder(@PathVariable Integer id){
        log.info("要删除的订单id: {}",id);
        orderService.deleteOrder(id);
        return "删除成功";
    }

    //修改订单
    @PutMapping("/")
    public Order modifyOrder(@RequestBody ModifyOrderRequest request){
        log.info("要修改的饮品: {}", request);
        //后期使用数据库update
        //1.通过查询id出饮品
        Order order= orderService.getOrderById(request.getId());
        order.setState(request.getState());
        // 3.更新数据库
        orderService.updateOrder(order);
        return order;
    }
}
